<template>
	<div class="direct-putaway-info">
		<div class="main_content">
			<div class="main_content_title">
				<div class="left">
					<span>{{ propUpType === 'add' ? $t('publicBtn.add') : propUpType === 'edit' ? $t('publicBtn.edit') :
						$t('publicBtn.detail') }}</span>
					<template v-for="(item, index) in $route.meta.title">
						<span v-if="languageId === item.langId">{{ item.name }}</span>
					</template>
				</div>
				<div class="right" />
			</div>
			<div class="main_content_con">
				<el-form ref="info" :model="info" :rules="rules" label-width="80px" class="demo-ruleForm">
					<div class="content_info">
						<template v-if="propUpType === 'add'">
							<el-row :gutter="24">
								<el-col :span="8">
									<el-form-item :label="$t('formLabel.supplier')" prop="supplierId">
										<el-select v-model="info.supplierId" clearable filterable :placeholder="$t('placeholderTip.pSelect')"
											style="width:100%">
											<el-option v-for="item in supplierList" :key="item.id" :label="item.name" :value="item.id">
											</el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :span="8">
									<el-form-item :label="$t('publicFormLabel.incomingWarehouse')" prop="warehouseId">
										<el-select v-model="info.warehouseId" clearable filterable :placeholder="$t('placeholderTip.pSelect')"
											style="width:100%">
											<el-option v-for="item in warehouseList" :key="item.id"
												:label="item.isSecond === 0 ? item.name + ' - ' + $t('publicFormLabel.newWarehouse') : item.name + $t('publicFormLabel.oldWarehouse')"
												:value="item.id">
											</el-option>
										</el-select>
									</el-form-item>
								</el-col>
							</el-row>
							<el-row :gutter="24">
								<el-col :span="8">
									<el-form-item :label="$t('publicFormLabel.intoNum')">
										<el-input readonly v-model="info.intoNum" :placeholder="$t('placeholderTip.bringOut')">
										</el-input>
									</el-form-item>
								</el-col>
							</el-row>
							<div class="line" />
							<div class="carDetails">
								<div>{{ $t('text.carDetails') }}</div>
								<div>
									<el-button type="primary" size="medium" @click="addData('forms')">{{ $t('publicBtn.add') }}</el-button>
								</div>
							</div>
							<!-- 新增车型数据结构 -->
							<div class='carContent_warp'>
								<table border="1" bordercolor="#dfe6ec" cellpadding="0" cellspacing="0">
									<thead>
										<tr style="height:40px;background: #f2f2f2;">
											<th style="width:90px">{{ $t('publicFormLabel.brandName') }}</th>
											<th style="width:90px">{{ $t('publicFormLabel.seriesName') }}</th>
											<th style="width:70px">{{ $t('publicFormLabel.carYear') }}</th>
											<th style="width:120px">{{ $t('publicFormLabel.modelName') }}</th>
											<th style="width:70px">{{ $t('publicFormLabel.nums') }}</th>
											<th style="width:150px"><span style="color:#FF2D55">*</span> {{ $t('publicFormLabel.outsideColours')
											}}</th>
											<th style="width:150px"><span style="color:#FF2D55">*</span> {{ $t('publicFormLabel.insideColours')
											}}</th>
											<th style="width:200px"><span style="color:#FF2D55">*</span>{{
												$t('publicFormLabel.carPurchaseUnitPrice') }}( {{ $t('text.priceUnit') }})
											</th>
											<th style="width:400px" v-if="isRetrofitting === 1">
												<div style="display: flex;justify-content: flex-start;">
													<div style="width: 180px;border-right:1px solid #999">{{ $t('publicFormLabel.optionalPackage')
													}}</div>
													<div style="width:150px">{{ $t('publicFormLabel.oPPurchasePrice') }}( {{ $t('text.priceUnit')
													}})</div>
												</div>
											</th>
											<th style="width:200px">{{ $t('publicFormLabel.overallPurchasePrice') }}( {{ $t('text.priceUnit')
											}})</th>
											<th style="width:260px"> <span style="color:#FF2D55">*</span> {{ $t('publicFormLabel.vinNumber') }}
											</th>
											<th style="width:260px">{{ $t('publicFormLabel.certificateNumber') }}</th>
											<th style="width:200px">{{ $t('publicFormLabel.engineNumber') }}</th>
											<th style="width:90px">{{ $t('publicFormLabel.operate') }}</th>
										</tr>
									</thead>
									<tbody v-if="forms.inventories.length > 0">
										<template v-for="(item, index) in forms.inventories">
											<tr v-if="forms.inventories.length > 0" :key="index + 'A'">
												<td :rowspan="item.stockInfoList.length + 1">{{ item.brandName }}</td>
												<td :rowspan="item.stockInfoList.length + 1">{{ item.seriesName }}</td>
												<td :rowspan="item.stockInfoList.length + 1">{{ item.carYear }}</td>
												<td :rowspan="item.stockInfoList.length + 1">{{ item.modelName }}</td>
												<td :rowspan="item.stockInfoList.length + 1">{{ item.intoNum }}</td>
												<td :rowspan="item.stockInfoList.length + 1">
													<el-select v-model="item.outsideId" clearable filterable
														:placeholder="$t('placeholderTip.pSelect')" style="width:100%">
														<el-option v-for="item in item.outsideList" :key="item.id" :label="item.name"
															:value="item.id">
														</el-option>
													</el-select>
												</td>
												<td :rowspan="item.stockInfoList.length + 1">
													<el-select v-model="item.insideId" clearable filterable
														:placeholder="$t('placeholderTip.pSelect')" style="width:100%%">
														<el-option v-for="item in item.insideList" :key="item.id" :label="item.name" :value="item.id">
														</el-option>
													</el-select>
												</td>
												<td :rowspan="item.stockInfoList.length + 1">
													<el-input type="number" min="0" v-model="item.carCost"
														:placeholder="$t('placeholderTip.enter')" />
												</td>
												<template>
													<td :rowspan="item.stockInfoList.length + 1" v-if="isRetrofitting === 1">
														<div v-for="(itemRetrofitt, indexRetrofitt) in item.retrofitList" style="padding: 5px 0;">
															<div style="display:flex;justify-content: flex-start">
																<el-select v-model="itemRetrofitt.retrofitId" clearable filterable
																	:placeholder="$t('placeholderTip.pSelect')" style="width:200px"
																	@change="getretrofitData($event, index, indexRetrofitt, item.retrofittingList_1)">
																	<el-option v-for="item in item.retrofittingList_1" :key="item.id"
																		:label="item.name + '-' + item.amount + $t('text.priceUnit')" :value="item.id">
																	</el-option>
																</el-select>
																<el-tooltip :content="itemRetrofitt.describe" placement="top"
																	v-if="itemRetrofitt.describe">
																	<i class="el-icon-info" style="color: #acadb1;"></i>
																</el-tooltip>
																<el-input type="number" min="0" v-model="itemRetrofitt.cost"
																	:placeholder="$t('placeholderTip.enter')"
																	style="width: 150px;margin-left:10px"></el-input>
																<el-button v-if="item.retrofitList.length > 1" type="danger" size="mini"
																	@click.stop="deletetRetrofittData(index, indexRetrofitt)"
																	style="margin-left:10px;height:26rpx;padding:0 10px;margin-top:5px">{{
																		$t('publicBtn.delete') }}</el-button>
															</div>
														</div>
														<!-- 新增选装按钮 -->
														<div style="margin-top: 10px;">
															<el-button size="medium" type="primary" @click="addRetrofittData(index)">{{
																$t('publicBtn.add') }}</el-button>
														</div>
													</td>
												</template>

												<!-- 整体采购单价(元) -->
												<td :rowspan="item.stockInfoList.length + 1">{{ item.totalCost }}</td>

												<!-- * VIN码、合格证号	发动机号	发动机号  删除 item.stockInfoList[0]  第1条数据展示  	 -->
												<template>
													<td>
														<el-input type="text" v-model="item.stockInfoList[0].vinNumber" maxlength="17" show-word-limit
															:placeholder="$t('placeholderTip.enter') + $t('text.vinNumberTip')"
															@keyup.native="btKeyUp($event, index, 0, 'vinNumber')"></el-input>
													</td>
													<td>
														<el-input type="text" v-model="item.stockInfoList[0].certificateNumber" maxlength="17"
															show-word-limit :placeholder="$t('placeholderTip.enter')"
															@keyup.native="btKeyUp($event, index, 0, 'certificateNumber')"></el-input>
													</td>
													<td>
														<el-input type="text" v-model="item.stockInfoList[0].engineNumber" maxlength="11"
															show-word-limit :placeholder="$t('placeholderTip.enter')"
															@keyup.native="btKeyUp($event, index, 0, 'engineNumber')"></el-input>
													</td>
													<td>
														<!-- 删除 item.stockInfoList.length 一条  删除整个item 对象 ,多条删除 item.stockInfoList中的某一条 -->
														<el-button v-if="item.stockInfoList.length === 1" type="danger" size="mini"
															@click.stop="deletetItemData(index)">{{ $t('publicBtn.delete') }}</el-button>
														<el-button v-if="item.stockInfoList.length > 1" type="danger" size="mini"
															@click.stop="deletetVin(index, 0)">{{ $t('publicBtn.delete') }}</el-button>
													</td>
												</template>
											</tr>

											<!-- item.stockInfoList 除第一条外的其他数据-->
											<template v-for="(itemStockInfos, indexStockInfos) in item.stockInfoList">
												<tr v-if="indexStockInfos !== 0" :key="index + 'c' + indexStockInfos + 'B'">
													<td>
														<el-input type="text" v-model="itemStockInfos.vinNumber" maxlength="17" show-word-limit
															:placeholder="$t('placeholderTip.enter') + $t('text.vinNumberTip')"
															@keyup.native="btKeyUp($event, index, indexStockInfos, 'vinNumber')"></el-input>
													</td>
													<td>
														<el-input type="text" v-model="itemStockInfos.certificateNumber" maxlength="17"
															show-word-limit :placeholder="$t('placeholderTip.enter')"
															@keyup.native="btKeyUp($event, index, indexStockInfos, 'certificateNumber')"></el-input>
													</td>
													<td>
														<el-input type="text" v-model="itemStockInfos.engineNumber" maxlength="11" show-word-limit
															:placeholder="$t('placeholderTip.enter')"
															@keyup.native="btKeyUp($event, index, indexStockInfos, 'engineNumber')"></el-input>
													</td>
													<td>
														<el-button type="danger" size="mini" @click.stop="deletetVin(index, indexStockInfos)">{{
															$t('publicBtn.delete') }}</el-button>
													</td>
												</tr>
												<!-- 最后一行显示 新增按钮 -->
												<tr v-if="indexStockInfos === item.stockInfoList.length - 1">
													<td colspan="4">
														<el-button style="width:120px" size="medium" type="primary" @click="addCarData(index)">{{
															$t('publicBtn.add') }}</el-button>
													</td>
												</tr>
											</template>

										</template>
									</tbody>
									<!-- 处理暂无数据 -->
									<tbody v-if="forms.inventories.length === 0" style="height:60px">
										<tr>
											<td :colspan="isRetrofitting === 1 ? 15 : 12">{{ $t('el.table.emptyText') }}</td>
										</tr>
									</tbody>
								</table>
							</div>
							<div class="line" />
							<!-- 审批流程  propup_type :1 add   -->
							<approvalProcessDetails :type=13 :propup_type=1 :list='approveList'></approvalProcessDetails>
						</template>
						<template v-if="propUpType === 'detail'">
							<el-row :gutter="24">
								<el-col :span="8">
									<el-form-item :label="$t('publicFormLabel.warehouseNumber')">
										<el-input readonly v-model="info.intoOrder" />
									</el-form-item>
								</el-col>
								<el-col :span="8">
									<el-form-item :label="$t('formLabel.supplier')">
										<el-input readonly v-model="info.supplierName" />
									</el-form-item>
								</el-col>
								<el-col :span="8">
									<el-form-item :label="$t('publicFormLabel.incomingWarehouse')">
										<el-input readonly v-model="info.warehouseName" />
									</el-form-item>
								</el-col>
							</el-row>
							<el-row :gutter="24">
								<el-col :span="8">
									<el-form-item :label="$t('publicFormLabel.intoNum')">
										<el-input readonly v-model="info.intoNum" />
									</el-form-item>
								</el-col>
								<el-col :span="8">
									<el-form-item :label="$t('publicFormLabel.warehousingTime')">
										<el-input readonly v-model="info.intoTime" />
									</el-form-item>
								</el-col>
							</el-row>
							<div class="line" />
							<div class="carDetails">
								<div>{{ $t('text.carDetails') }}</div>
							</div>
							<div class='carContent_warp'>
								<table border="1" bordercolor="#dfe6ec" cellpadding="0" cellspacing="0">
									<thead>
										<tr style="height:40px;background: #f2f2f2;">
											<th style="width:90px">{{ $t('publicFormLabel.brandName') }}</th>
											<th style="width:90px">{{ $t('publicFormLabel.seriesName') }}</th>
											<th style="width:70px">{{ $t('publicFormLabel.carYear') }}</th>
											<th style="width:120px">{{ $t('publicFormLabel.modelName') }}</th>
											<th style="width:70px">{{ $t('publicFormLabel.nums') }}</th>
											<th style="width:130px"><span style="color:#FF2D55">*</span> {{ $t('publicFormLabel.outsideColours')
											}}</th>
											<th style="width:130px"><span style="color:#FF2D55">*</span> {{ $t('publicFormLabel.insideColours')
											}}</th>
											<th style="width:200px"><span style="color:#FF2D55">*</span> {{
												$t('publicFormLabel.carPurchaseUnitPrice') }}( {{ $t('text.priceUnit') }})
											</th>
											<th style="width:300px" v-if="isRetrofitting === 1">
												<div style="display: flex;justify-content: center;">
													{{ $t('publicFormLabel.optionalPackage') }} | {{ $t('publicFormLabel.oPPurchasePrice') }}
													( {{ $t('text.priceUnit') }})
												</div>
											</th>
											<th style="width:200px">{{ $t('publicFormLabel.overallPurchasePrice') }}( {{ $t('text.priceUnit')
											}})</th>
											<th style="width:240px"> <span style="color:#FF2D55">*</span> {{ $t('publicFormLabel.vinNumber') }}
											</th>
											<th style="width:240px">{{ $t('publicFormLabel.certificateNumber') }}</th>
											<th style="width:180px">{{ $t('publicFormLabel.engineNumber') }}</th>
										</tr>
									</thead>
									<tbody v-if="forms.inventories.length > 0">
										<template v-for="(item, index) in forms.inventories">
											<tr v-if="forms.inventories.length > 0" :key="index + 'A'">
												<td :rowspan="item.stockInfoList.length">{{ item.brandName }}</td>
												<td :rowspan="item.stockInfoList.length">{{ item.seriesName }}</td>
												<td :rowspan="item.stockInfoList.length">{{ item.carYear }}</td>
												<td :rowspan="item.stockInfoList.length">{{ item.modelName }}</td>
												<td :rowspan="item.stockInfoList.length">{{ item.intoNum }}</td>
												<td :rowspan="item.stockInfoList.length">
													{{ item.outsideColour }}
												</td>
												<td :rowspan="item.stockInfoList.length">
													{{ item.insideColour }}
												</td>
												<td :rowspan="item.stockInfoList.length">{{ item.carCost || 0 }}</td>
												<td :rowspan="item.stockInfoList.length" v-if="isRetrofitting === 1">
													<div style="display:block;" v-if="item.retrofitList">
														<div v-for="(item1, index1) in item.retrofitList" :key="index1"
															style="display: flex;justify-content: space-between;align-items: center;padding:5px 0">
															<div style="flex: 1;">
																{{ item1.name }}
																<el-tooltip :content="item1.describe" placement="top" v-if="item1.describe">
																	<i class="el-icon-info" style="color: #acadb1;"></i>
																</el-tooltip>
															</div>
															<span style="margin-left: 20px;flex: none;"> {{ item1.cost || 0 }} </span>
														</div>
													</div>
												</td>
												<!-- 整体采购价 -->
												<td :rowspan="item.stockInfoList.length">{{ item.totalCost }}</td>
												<!-- * VIN码、合格证号	发动机号	发动机号  第0个	 -->
												<td>{{ item.stockInfoList[0].vinNumber }}</td>
												<td>{{ item.stockInfoList[0].certificateNumber }}</td>
												<td>{{ item.stockInfoList[0].engineNumber }}</td>
											</tr>
											<template v-for="(itemStockInfos, indexStockInfos) in item.stockInfoList">
												<tr v-if="indexStockInfos !== 0" :key="index + 'A' + indexStockInfos + 'B'">
													<td>{{ itemStockInfos.vinNumber }}</td>
													<td>{{ itemStockInfos.certificateNumber }}</td>
													<td>{{ itemStockInfos.engineNumber }}</td>
												</tr>
											</template>
										</template>
									</tbody>

								</table>
							</div>

							<div class="line" />
							<!-- 审批流程  propup_type 2---detail-->
							<approvalProcessDetails v-if="propUpType === 'detail'" :type=13 :propup_type=2 :list='approveList'>
							</approvalProcessDetails>
						</template>
					</div>
					<div class="footerBtn">
						<el-button @click="getCancle">{{ propUpType !== 'detail' ? $t('el.popconfirm.cancelButtonText') :
							$t('el.popconfirm.close') }}</el-button>
						<el-button v-if="propUpType !== 'detail'" type="primary" @click="addSubmitForm"
							v-loading.fullscreen.lock="fullscreenLoading">{{ $t('el.popconfirm.confirmButtonText') }}</el-button>
					</div>
				</el-form>
			</div>
		</div>
		<!-- 添加车型 -->
		<vehicleModel ref="vehicleModel"></vehicleModel>
	</div>
</template>
<script>
import {
	getSupplierSupplierList
} from '@/api/basics-manager'
import {
	getRetrofitSelectList,
	getColourList
} from '@/api/system-manager'
import {
	getIntoInfo,
	setIntoSave,
} from '@/api/commercial-vehicle-manager'
import { getPresaleAttributeList } from '@/api/public'
import approvalProcessDetails from '@/components/approval-process-details'
import vehicleModel from '@/components/vehicle-model'

import { getToken } from '@/utils/auth'
export default {
	name: 'DirectPutAwayInfo',
	components: {
		vehicleModel,
		approvalProcessDetails, //审批
	},
	data() {
		return {
			mdPagination: {
				page: 1,
				limit: 20,
				total: 0
			}, //车型字典分页
			isRetrofitting: this.$store.getters.isRetrofitting, //是否开启选装 1开启 2关闭
			info: {
				intoNum: '', //入库数量
				supplierId: '',
				supplierName: '', //供应商
				warehouseName: '', //仓库
				warehouseId: ''
			},
			// 车辆明细
			forms: {
				inventories: []
			},
			supplierList: [], //供应商
			warehouseList: [], //仓库
			rules: {
				warehouseId: [
					{
						required: true,
						message: this.$t('placeholderTip.pSelect'),
						trigger: 'change'
					}
				],
				supplierId: [
					{
						required: true,
						message: this.$t('placeholderTip.pSelect'),
						trigger: 'change'
					}
				]
			},
			approveList: [], //详情返回审批
			propUpType: '', // 'add' edit detail
			token: getToken(),
			loading: false,
			showSearchFlag: true, //车型字典搜索
			fullscreenLoading: false,
		}
	},
	watch: {
		'forms.inventories': {
			handler: function (val) {
				if (val.length > 0) {
					if (this.propUpType === 'add') {
						var sumNumber = 0
						var number = []
						for (var i = 0; i < val.length; i++) {
							var cost = 0
							var price = 0
							var item = val[i]
							// 选装包采购价格处理
							if (item.retrofitList.length > 0) {
								item.retrofitList.map(i => {
									if (i.cost) {
										cost = cost + Number(i.cost)
									}
									// 选装包售价价格处理
									if (i.amount) {
										price = price + Number(i.amount)
									}
								})
							}
							item.retrofitCost = Number(cost) //选装包总价
							item.totalCost = Number(item.carCost || 0) + Number(cost) //整体采购价
							number.push(Number(item.intoNum))
						}
						number.forEach(item => {
							sumNumber = sumNumber + item
						})
						this.info.intoNum = sumNumber
					}
				} else {
					if (this.propUpType === 'add') {
						this.info.intoNum = 0
					}
				}
			},
			deep: true
		}
	},
	computed: {
		languageId() {
			// 固定语种ID  1:中文  2:英文 3:俄文 4:乌语
			return this.$store.getters.languageId
		},
	},
	mounted() {
		window._vm = this
		this.propUpType = this.$route.query.propUpType
		if (this.$route.query.propUpType === 'add') {
			this.getSupplierSupplierList() //供应商
			this.getWarehouseListData() //仓库
		} else if (this.$route.query.id) {
			this.getInfo(this.$route.query.id)
		}
	},
	created() { },
	activated() { },
	destroyed() { },
	methods: {
		// 禁止input输入框输入特殊字符和汉字
		btKeyUp(e, index, index2, key) {
			this.forms.inventories[index].stockInfoList[index2][
				key
			] = e.target.value.replace(/[^A-Z0-9]/g, '')
		},
		// 仓库列表
		getWarehouseListData() {
			// 公共接口 仓库 ids 13
			getPresaleAttributeList(13).then(res => {
				if (res.code === 200) {
					this.warehouseList = res.data.warehouseList || []
				}
			})
		},
		// 供应商
		getSupplierSupplierList() {
			getSupplierSupplierList().then(res => {
				if (res.code === 200) {
					this.supplierList = res.data
				}
			})
		},
		addData() {
			this.$refs.vehicleModel.getOpen()
		},
		getvehicleModelId(row) {
			var obj = JSON.parse(row)
			// 对象 新加的数据
			obj['carCost'] = '' //裸车采购单价
			obj['intoNum'] = 1 //入库数量
			obj['insideList'] = []
			obj['outsideList'] = []
			obj['retrofittingList_1'] = [] //选装包list  提交时删除此字段
			obj['totalCost'] = '' //整体采购单价(元)
			obj['retrofittingId'] = ''//选转包id 数组整合 提交时删除此字段
			obj['retrofitCost'] = '' //选装包总价
			// 选装包多选
			obj['retrofitList'] = [
				{
					describe: '', ////选装包描述
					amount: '', //选装包售价(元)
					cost: '', //选装包单价
					retrofitId: '',//选装包id
					name: ''
				}
			]
			// VIN码 合格证 、发动机
			obj['stockInfoList'] = [
				{
					certificateNumber: '', //合格证号
					engineNumber: '', //发动机号
					vinNumber: '' //VIN码
				}
			]
			this.$nextTick(() => {
				// 1:外观 2:内饰
				getColourList(`type=1&modelId=${obj.modelId}`).then(res => {
					if (res.code === 200) {
						if (res.data) {
							obj.outsideList = res.data
						}
					}
				})
				getColourList(`type=2&modelId=${obj.modelId}`).then(res => {
					if (res.code === 200) {
						if (res.data) {
							obj.insideList = res.data
						}
					}
				})

				// 选装包
				getRetrofitSelectList(`modelId=${obj.modelId}`).then(res => {
					if (res.code === 200) {
						if (res.data) {
							obj.retrofittingList_1 = res.data || []
						}
					}
				})
			})
			this.forms.inventories.push(obj)
			this.isTypeDate = true
		},

		// 选装包list数据变化
		getretrofitData(val, index, z_index, retrofitList) {
			if (val) {
				retrofitList.filter(item => {
					if (val === item.id) {
						this.forms.inventories[index].retrofitList[
							z_index
						].describe = item.describe

						this.forms.inventories[index].retrofitList[z_index].amount =
							item.amount
						this.forms.inventories[index].retrofitList[z_index].name =
							item.name
						this.forms.inventories[index].retrofitList[z_index].cost =
							item.cost || ''
					}
				})
				this.$forceUpdate()
			} else {
				this.forms.inventories[index].retrofitList[z_index].amount = ''
				this.forms.inventories[index].retrofitList[z_index].name = ''
				this.forms.inventories[index].retrofitList[z_index].describe =
					''
				this.forms.inventories[index].retrofitList[z_index].cost = ''
				this.$forceUpdate()
			}
		},
		// 删除车型
		deletetItemData(index) {
			this.forms.inventories.splice(index, 1)
			this.$forceUpdate() // 强制跟新
		},
		// 删除车型vin码
		deletetVin(index, index2) {
			this.forms.inventories[index].stockInfoList.splice(index2, 1)
			this.$forceUpdate() // 强制跟新
			this.getCheckedInventoriesDelete(index)
		},
		// 减1
		getCheckedInventoriesDelete(index) {
			//车辆明细数组中入库数量 （已入库 /总量）在已入库基础上加 -1
			this.forms.inventories[index].intoNum =
				this.forms.inventories[index].intoNum - 1
			// 入库数量  当前采购单入库数量
			this.info.intoNum = this.info.intoNum - 1
		},
		// 添加选装包多选
		addRetrofittData(index) {
			var obj = {
				describe: '', ////选装包描述
				amount: '', //选装包售价(元)
				cost: '', //选装包单价
				retrofitId: '',
				name: ''
			}
			this.forms.inventories[index].retrofitList.push(obj)
		},
		// 删除选装包
		deletetRetrofittData(index, z_index) {
			this.forms.inventories[index].retrofitList.splice(z_index, 1)
			this.$forceUpdate() // 强制跟新
		},
		//添加车型vin码
		addCarData(index) {
			var obj = {
				certificateNumber: '', //合格证号
				engineNumber: '', //发动机号
				vinNumber: '' //VIN码
			}
			this.forms.inventories[index].stockInfoList.push(obj)
			this.getCheckedInventoriesAdd(index)
		},
		// 加1
		getCheckedInventoriesAdd(index) {
			//车辆明细数组中入库数量 （已入库 /总量）在已入库基础上加1
			this.forms.inventories[index].intoNum =
				this.forms.inventories[index].intoNum + 1
			// 入库数量  当前采购单入库数量
			this.info.intoNum = this.info.intoNum + 1
		},

		// 提交
		addSubmitForm() {
			this.$refs['info'].validate(valid => {
				if (valid) {
					var obj = JSON.parse(JSON.stringify(this.info))
					var list = JSON.parse(JSON.stringify(this.forms.inventories))
					var flag = this.getCheckData(list)
					this.supplierList.filter(i => {
						if (i.id === obj.supplierId) {
							obj.supplierName = i.name
						}
					})
					this.warehouseList.filter(i => {
						if (i.id === obj.warehouseId) {
							obj.warehouseName = i.name
						}
					})

					if (flag) {
						list.filter(i => {
							delete i['retrofittingList_1'] //删除 每对象的获取选装的list
							delete i['retrofittingId'] //删除 每对象的获取选装重组id
						})
						this.$confirm(this.$t('text.carDetailsTip'), this.$t('el.messagebox.title'), {
							confirmButtonText: this.$t('el.messagebox.confirm'),
							cancelButtonText: this.$t('el.messagebox.cancel'),
							type: 'warning'
						})
							.then(() => {
								this.fullscreenLoading = true
								var params = {
									intoNum: obj.intoNum, //入库数量
									supplierId: obj.supplierId,
									supplierName: obj.supplierName, //供应商
									warehouseName: obj.warehouseName, //仓库
									warehouseId: obj.warehouseId,
									inventories: list //车辆明细
								}
								if (this.propUpType === 'add') {
									setIntoSave(params)
										.then(res => {
											if (res.code === 200) {
												this.$message({
													showClose: true,
													message: this.$t('text.addSucceedTip'),
													type: 'success'
												})
												this.fullscreenLoading = false
												this.getCancle()
											}
										})
										.catch(err => {
											this.fullscreenLoading = false
											console.log(err, 'err')
										})
								}
							})
							.catch(() => { })
					}
				} else {
					return false
				}
			})
		},

		getCheckData(arr) {
			var list = arr
			if (list.length > 0) {
				var vinNumbers = []
				var certificateNumbers = []
				var engineNumbers = []
				var retrofittingIds = []
				// 新增数据 校验
				// 判断 外饰、内饰 裸车采购单价 选装包采购单价 是否必填
				// 判断 外饰、内饰、选装存在重复
				console.log(this.isTypeDate)
				if (this.isTypeDate) {
					for (var i = 0; i < list.length; i++) {
						var item = list[i]
						if (item.outsideId && item.insideId) {
						} else {
							this.$message({
								message: this.$t('text.colorsTip'),
								type: 'warning'
							})
							return false
						}
					}
					if (list.length > 1) {
						var _this = this
						// 判断 车型 外饰、内饰 是否必填
						// 判断 车型 外饰、内饰、选装存在重复
						list.map(i => {
							i.retrofittingId = []
							if (i.retrofitList.length > 0) {
								i.retrofitList.map(j => {
									if (j.retrofitId) {
										i.retrofittingId.push(j.retrofitId)
									}
								})
							}
							i.retrofittingId.sort()
						})
						const newArr = list.reduce(function (tempArr, item) {
							// 选装开启
							if (_this.isRetrofitting === 1) {
								//处理选装 组装选装ids
								var itemRetrofitIds = []
								if (item && item.retrofitList.length > 0) {
									item.retrofitList.filter(i => {
										if (i.retrofitId) {
											itemRetrofitIds.push(i.retrofitId)
										}
									})
									itemRetrofitIds.sort()
								}
								if (
									tempArr.findIndex(
										ele =>
											ele.modelId === item.modelId &&
											ele.outsideId === item.outsideId &&
											ele.insideId === item.insideId &&
											ele.retrofittingId.toString() === itemRetrofitIds.toString()
									) === -1
								) {
									tempArr.push(item)
								}
								return tempArr
							} else {
								if (
									tempArr.findIndex(
										ele =>
											ele.modelId === item.modelId &&
											ele.outsideId === item.outsideId &&
											ele.insideId === item.insideId
									) === -1
								) {
									tempArr.push(item)
								}
								return tempArr
							}
						}, [])
						if (newArr.length !== list.length) {
							this.$message({
								message: this.$t('text.modelColors') + `${_this.isRetrofitting === 1 ? (this.$t('text.orTip') + this.$t('publicFormLabel.optionalPackage')) : ''}` + this.$t('text.itemsRepeatTip'),
								type: 'warning'
							})
							return false
						}
					}
				}
				//处理 裸车采购单价 选装包采购单价
				// 处理VIN码 必填校验  及 长度 是否重复
				for (var i = 0; i < list.length; i++) {
					var item_1 = list[i]
					if (!item_1.carCost || item_1.carCost === '') {
						this.$message({
							message: this.$t('text.enterCarCostTip'),
							type: 'warning'
						})
						return false
					}
					if (item_1.carCost <= 0) {
						this.$message({
							message: this.$t('text.isEnterCarCostTip'),
							type: 'warning'
						})
						return false
					}

					var item2 = list[i].retrofitList
					retrofittingIds = []
					if (item2.length > 0) {
						for (var z = 0; z < item2.length; z++) {
							var u = item2[z]
							if (u.retrofitId && (!u.cost || u.cost === '')) {
								this.$message({
									message: this.$t('text.enterRetrofittTip'),
									type: 'warning'
								})
								return false
							}
							if (u.retrofitId && u.cost < 0) {
								this.$message({
									message: this.$t('text.isEnterRetrofittTip'),
									type: 'warning'
								})
								return false
							}
							if (u.retrofitId) {
								retrofittingIds.push(u.retrofitId)
							}
						}
						// 选装包重复校验 不同车型的 选装包不同
						var xzb_flag = this.getFilterData(
							retrofittingIds,
							this.$t('publicFormLabel.optionalPackage'),
							item_1.retrofittingList_1
						)
						if (!xzb_flag) {
							return false
						}
						item_1.retrofittingId = retrofittingIds.toString() //单独处理 选装包id
					}
					var item = list[i].stockInfoList
					if (item.length > 0) {
						for (var j = 0; j < item.length; j++) {
							if (!item[j].vinNumber) {
								this.$message({
									message: this.$t('text.enterVin') + `,${i + 1}${this.$t('text.group')},${j + 1}${this.$t('text.article')}`,
									type: 'warning'
								})
								return false
							}
							if (item[j].vinNumber.length <= 16) {
								this.$message({
									message: this.$t('text.isEnterVIn') + `,${i + 1}${this.$t('text.group')},${j + 1}${this.$t('text.article')}`,
									type: 'warning'
								})
								return false
							}
							// 合格证号
							vinNumbers.push(item[j].vinNumber)
							if (item[j].certificateNumber) {
								certificateNumbers.push(item[j].certificateNumber)
							}
							// 发动机号
							if (item[j].engineNumber) {
								engineNumbers.push(item[j].engineNumber)
							}
						}
					}
				}
				// 整个数组不可能出现 相同的 vIN码、合格证号、发动机号
				var flag = this.getFilterData(vinNumbers, this.$t('publicFormLabel.vinNumber')) //vIN码 重复校验
				if (flag) {
					var flag1 = true
					var flag2 = true
					if (certificateNumbers.length > 0) {
						// 合格证号 重复校验
						flag1 = this.getFilterData(certificateNumbers, this.$t('publicFormLabel.certificateNumber'))
					}
					if (flag1) {
						//发动机号 重复校验
						if (engineNumbers.length > 0) {
							flag2 = this.getFilterData(engineNumbers, this.$t('publicFormLabel.engineNumber'))
						}
					}
					return flag && flag1 && flag2
				} else {
					return false
				}
			} else {
				this.$message({
					message: this.$t('text.improveVehicCarInfo'),
					type: 'warning'
				})
				return false
			}
		},
		// 字段重复校验
		getFilterData(ary, label, retrofitList) {
			var copyList = []
			var nary = ary.sort()
			for (var i = 0; i < ary.length; i++) {
				if (nary[i] == nary[i + 1]) {
					copyList.push(nary[i])
				}
			}

			if (copyList.length > 0) {
				// 选装提示 名称处理
				var copyList_names = []
				if (retrofitList) {
					for (var j = 0; j < copyList.length; j++) {
						for (var z = 0; z < retrofitList.length; z++) {
							var ele = retrofitList[z]
							if (copyList[j] == ele.id) {
								copyList_names.push(ele.name)
							}
						}
					}
				}
				if (copyList_names.length > 0) {
					this.$message({
						message: `${label}${this.$t('text.itemsRepeatTip')}:${copyList_names.toString()}`,
						type: 'warning'
					})
					return false
				} else {
					this.$message({
						message: `${label}${this.$t('text.itemsRepeatTip')}:${copyList.toString()}`,
						type: 'warning'
					})
					return false
				}
			}
			return true
		},
		// 详情
		getInfo(id) {
			getIntoInfo(id).then(res => {
				if (res.code === 200) {
					var obj = res.data
					if (obj.recordInfo.processInfo) {
						this.approveList = obj.recordInfo.processInfo
					}
					this.forms.inventories = obj.inventories
					this.info = obj
				}
			})
		},

		// 取消
		getCancle() {
			this.$store.state.tagsView.visitedViews.splice(
				this.$store.state.tagsView.visitedViews.findIndex(
					item => item.path === this.$route.path
				),
				1
			)
			this.$router.go(-1)
		}
	}
}
</script>
<style lang="scss" scoped>
.direct-putaway-info {
	height: 100%;
	width: 100%;
	padding: 20px;
	box-sizing: border-box;
	overflow: hidden;

	&>.main_content {
		width: 99.9%;
		height: 100%;

		&>.main_content_title {
			margin-bottom: 10px;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
		}

		.main_content_con {
			height: calc(100% - 40px);
			width: 100%;
			display: inline-block;
			border: 1px solid #dfe6ec;
			padding: 20px;
			overflow: hidden;

			&>.el-form {
				width: 100%;
				height: 100%;

				&>.content_info {
					width: 100%;
					height: calc(100% - 60px);
					overflow: hidden;
					overflow-y: auto;
				}

				.footerBtn {
					width: 100%;
					display: flex;
					justify-content: center;
					margin-top: 20px;
				}
			}

			.line {
				height: 1px;
				margin-bottom: 25px;
				background: #ebeef5;
			}

			.carContent_warp {
				min-width: 99%;
				height: auto;
				overflow: auto;
			}

			.carDetails {
				width: 100%;
				margin-bottom: 20px;
				display: flex;
				justify-content: space-between;
				align-items: center;

				&>div {
					width: auto;
				}
			}

			table {
				display: table;
				width: 100%;
				border-collapse: collapse;
				color: #606266;
				font-size: 14px;
				table-layout: fixed;
				overflow-y: scroll;

				th,
				td {
					padding: 10px;
					text-align: center;
				}

				td {
					width: 100%;
					overflow: hidden;
					// text-overflow: ellipsis; //文字换行
					word-break: break-all; //数字字母换行
				}
			}

			.el-button:hover,
			.el-button:focus {
				color: #000;
			}
		}
	}
}
</style>
<style lang="scss" scoped>
.main_content_con_md {
	width: 100%;
	height: calc(100% - 25px);
	overflow: auto;
	display: inline-block;

	::v-deep .el-table__body tr.current-row>td {
		background-color: #e6c892 !important;
	}

	.el-table {
		::v-deep .el-icon-arrow-right {
			font-weight: 600;
		}

		::v-deep .el-form-item__error {
			left: 5%;
		}

		::v-deep .el-table__header .el-checkbox {
			display: none !important;
		}

		::v-deep th {
			padding: unset;
			background-color: #f2f2f2;

			.header_item {
				height: 39px;
				line-height: 39px;
				display: flex;
				justify-content: center;
				align-items: center;

				&>.keySort {
					width: 23px;
					height: 21px;
					position: relative;
					display: inline-block;

					&>.el-icon-caret-top {
						position: absolute;
						top: 0;
						right: 0;
						left: 0;
						margin: auto;
						cursor: pointer;
					}

					&>.el-icon-caret-bottom {
						position: absolute;
						bottom: 0;
						right: 0;
						left: 0;
						margin: auto;
						cursor: pointer;
					}
				}
			}
		}
	}
}
</style>
<style lang="scss" scoped>
::v-deep .add_dialog {
	height: calc(100% - 30vh) !important;
	position: relative;

	.el-dialog__header {
		border-bottom: 1px solid #e4e4e4;
		text-align: left;
	}

	.el-dialog__body {
		height: calc(100% - 125px) !important;
	}

	.el-dialog__footer {
		border-top: 1px solid #e4e4e4;
		text-align: right;
	}

	.el-button:hover,
	.el-button:focus {
		color: #000;
	}
}
</style>


